<?php
require_once '../wc/pscws3.class.php';
class overseaSearchEngine {
	
	public $cutWordCWS;
	/*the original keyword from the frontend input by user or passed by system default*/
	public $usrInputKeyword;
	/*the array to store the keywords which have been cut/fliter with the CWS*/
	public $keywordArr; 
	/*the string used to search in the db*/
	public $keywordStr; 
	/*the indicator to determine  the searching scope for the keyword*/
	protected $searchType; 
	public $displayLimitation = '8'; /*default to show 5 records only*/
	
	/* for Page Operation */
	public $offset;
	public $rowsCount;
	public $prevPage;
	public $nextPage;
	public $currPage;
	public $totalPage;
	public $totalResultNum;
	public $recordsPerPageSetting = '35';
	
	function __construct($keyword,$dict='../wc/dict/dict.xdb',$filterMark = true) {
		/*initilization of the CWS (open source plugin) */
	//	$this -> cutWordCWS = new PSCWS3($dict);
	//	$this -> cutWordCWS ->set_ignore_mark($filterMark);
		$this->usrInputKeyword = $keyword;
		self::searchKeywordInit($this->usrInputKeyword);
	}
	
	public function searchKeywordInit($keyword){
		//$this -> keywordArr = $this -> cutWordCWS ->segment(convertRNToNill(strip_tags($keyword)));
		$this -> keywordArr = sp_str($keyword);
		//print_r($this->keywordArr);
		if(is_array($this -> keywordArr)&&count($this -> keywordArr)>1){
			foreach ($this -> keywordArr as $val){
				$tmp[] = quweima($val);
			}
			$extraKeyWord = implode(' ',$tmp);
			$this -> keywordStr =$extraKeyWord;
		}else{
			$this -> keywordStr = quweima(($keyword));
		}
	}
	
	public function startUpSearchKeyword(CsDatabaseOpera $dbObj,$sqlCondition)
	{
		/*Using Group By To Filter the Protencial duplicated records*/
		switch($this->searchType)
		{
			default:
				$sql = "SELECT * FROM (SELECT `rel_id`,`obj_cat`,`usr_id`,MATCH(a.content) AGAINST('".$this -> keywordStr."') AS score FROM `cs_engine` a WHERE $sqlCondition MATCH(a.content) AGAINST('".mysql_real_escape_string($this -> keywordStr)."')) bb GROUP BY `rel_id`,`obj_cat` order by `score` DESC";
		}
		$sql = self::limitSqlPhase($dbObj,$sql);
		
		$tResultSet = $dbObj->executeQuery($sql);
		return $tResultSet;
	}
	
	public function asynSearchRecords(CsDatabaseOpera $dbObj,$sqlCondition)
	{
		switch($this->searchType)
		{
			default:
				$sql = "SELECT * FROM (SELECT `rel_id`,`obj_cat`,`usr_id`,MATCH(a.content) AGAINST('".$this -> keywordStr."') AS score FROM `cs_engine` a WHERE $sqlCondition MATCH(a.content) AGAINST('".mysql_real_escape_string($this -> keywordStr)."')) bb GROUP BY `rel_id`,`obj_cat` order by `obj_cat` DESC LIMIT 0,$this->displayLimitation";
		}
		$tResultSet = $dbObj->executeQuery($sql);
		return $tResultSet;
	}
	
	public function setSearchType($type)
	{
		$this->searchType = $type;
	}
	
	public function highLightSearchedKeywords($str,$word)
	{
		global $sepKeyWordArr;
		/*to define how many words should be cut*/
		$cutStrPt = 140;
		/**/
		$remain = strstr($str,$word); 
		/*get the position of the 1st appearance of the keyword*/
		$index = strpos($str,$word);
		
		if(!$index){
			$max = 0;
			/*if can not locate the entrie keyword , then separarate the str*/
			if(is_array($this -> keywordArr)&&count($this -> keywordArr)>0){
				foreach ($this -> keywordArr as $val){
					/*if there is only one character then skip the steps*/
					if(strlen($val)<=2) continue;
					$tmpIndex = strpos($str,$word);
					$max = ($tmpIndex > $max)?$tmpIndex:$max;
				}
			}
			$index = $max;
		}else{
			/**
			 * Add by Cavin 2012,July 16
			 * Desp: hightlighted the entire keyword if exists
			 */
			$str = str_ireplace($word,"<font color='red'>".$word."</font>",$str);
		}
		
		if($index > $cutStrPt){
			/*if the keyword is very inside the text, then cut both at the front and back*/
			$text = subString($str,0,80).'...'.subString($str,$index-40,$index-2).subString($remain,$index,80).'...';
		}else{
			/*the entrie keyword is at the very begginning then just cut a bit of the front*/
			$text = subString($str,0,$cutStrPt);
			if(strlen($str) > $cutStrPt){
				$text.='...';
			}
		}
		return highLightSearchedKeywords($text,$this -> keywordArr);
	}
	
	public function pageConfigSetting($currPage,$rowsCount='')
	{
		$this -> rowsCount = ($rowsCount == '') ? $this -> recordsPerPageSetting : $rowsCount;
		$this -> offset = ($currPage - 1) * $this -> rowsCount;
		$this -> prevPage = ($currPage == 1) ? 1 : $currPage - 1;
		$this -> nextPage = $currPage + 1;
		$this -> currPage = $currPage;
	}
	
	/*
	 * Function:limitSqlPhase
	 * Desp: combine the SQL phase with the limitation in the suffix
	 * Params: 1st DB Object; 2nd Original SQL
	 */
	public function limitSqlPhase(CsDatabaseOpera $dbObj, $sql)
	{
		$this -> totalResultNum = $dbObj->getQueryNum ( $sql );
		$this -> totalPages = ceil ( $this -> totalResultNum / $this -> rowsCount );
		$this -> nextPage = ($this -> nextPage > $this -> totalPages) ? $this -> totalPages : $this -> nextPage;
		$sql .= " LIMIT {$this -> offset},{$this -> rowsCount}"; 
		return $sql;
	}
	
}

?>